import GChild from "../../../Core/GView/GChild";
import MathEx, { Plane } from "../../../Core/Math/MathEx";
import BtlCameraMap from "./BtlCameraMap";



const { ccclass, property, menu } = cc._decorator;
@ccclass
export default class BtlMapTouchLayer extends GChild {

	protected _plane: Plane;
	protected _isTouchBegin = false;
	private _map: BtlCameraMap = null;
	public set map(map: BtlCameraMap) {
		this._map = map;
	}
	public get map(): BtlCameraMap {
		return this._map;
	}

	onLoad() {
		this.node.position = cc.Vec2.ZERO;
		this.node.setContentSize(cc.winSize.width, cc.winSize.height);
		this._plane = new Plane;
		this._plane.p = new cc.Vec3(0, 0, 0);
		this._plane.n = new cc.Vec3(0, 0, 1);

		this.node.on<{ (event: cc.Event.EventTouch): void }>(cc.Node.EventType.TOUCH_START, this.onTouchBegin, this);
		this.node.on<{ (event: cc.Event.EventTouch): void }>(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
		this.node.on<{ (event: cc.Event.EventTouch): void }>(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
		this.node.on<{ (event: cc.Event.EventTouch): void }>(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
		this.node.on<{ (event: cc.Event.EventMouse): void }>(cc.Node.EventType.MOUSE_WHEEL, this.onMouseWheel, this);
	}

	/**触摸开始 */
	protected onTouchBegin(event: cc.Event.EventTouch) {
	}

	/**触摸移动 */
	protected onTouchMove(event: cc.Event.EventTouch) {
	}

	/**触摸结束 */
	protected onTouchEnd(event: cc.Event.EventTouch) {
	}

	/**触摸取消 */
	protected onTouchCancel(event: cc.Event.EventTouch) {
	}

	/**鼠标滚轮移动监听 */
	protected onMouseWheel(event: cc.Event.EventMouse) {
	}

	/**获取触碰点的坐标 */
	public getTouchWorldPos(pos: cc.Vec2) {
		let ray = this.map.camera.getRay(pos);
		let world = MathEx.rayPlaneIntersectPoint(ray, this._plane);
		return world;
	}

	onDestroy() {
		this.node.targetOff(this);
	}
}